package hht.dragon.blog.mapper;

import hht.dragon.blog.model.Article;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 文章表的数据库操作.
 * User: huang
 * Date: 18-6-23
 */
@Repository
public interface ArticleMapper {

    /**
     * 获取浏览最多的几篇文章.
     * @param pageSize 文章数量
     * @return
     */
    @Select("select * from article order by look_counter desc limit 0, #{pageSize}")
    @Results({
            @Result(id = true, property = "articleId", column = "article_id"),
            @Result(property = "types", column = "article_id",
            many = @Many(select = "hht.dragon.blog.mapper.TypeMapper.getTypesByArticle"))
    })
    List<Article> getHotArticle(@Param("pageSize") int pageSize);

    /**
     * 获取所有文章.
     * @return
     */
    @Select("select * from article order by create_date desc")
    @Results({
            @Result(id = true, property = "articleId", column = "article_id"),
            @Result(property = "types", column = "article_id",
            many = @Many(select = "hht.dragon.blog.mapper.TypeMapper.getTypesByArticle"))
    })
    List<Article> getAllArticle();

    @Select("select * from article where article_id in (select article_id from type_article where type_id = #{typeId}) order by create_date desc")
    @Results({
            @Result(id = true, property = "articleId", column = "article_id"),
            @Result(property = "types", column = "article_id",
                    many = @Many(select = "hht.dragon.blog.mapper.TypeMapper.getTypesByArticle"))
    })
    List<Article> getAllTypeArticle(int typeId);

    /**
     * 通过id获取文章.
     * @param articleId 文章id
     * @return
     */
    @Select("select * from article where article_id = #{articleId}")
    @Results({
            @Result(id = true, property = "articleId", column = "article_id"),
            @Result(property = "types", column = "article_id",
                    many = @Many(select = "hht.dragon.blog.mapper.TypeMapper.getTypesByArticle"))
    })
    Article getArticleById(int articleId);

    /**
     * 保存文章数据，并返回id存入article实例的articleId属性中.
     * @param article
     * @return
     */
    @Insert("insert into article(article_title, create_date, article_context)" +
            " value(#{article.articleTitle}, #{article.createDate}, #{article.articleContext})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "article.articleId", resultType = int.class, before = false )
    @Options(useGeneratedKeys = true, keyColumn = "article.articleId")
    int save(@Param("article") Article article);

    /**
     * 保存类型与文章之间的关系.
     * @param typeId 类型id
     * @param articleId 文章id
     * @return
     */
    @Insert("insert into type_article(type_id, article_id) value(#{typeId}, #{articleId})")
    int saveTypeArticle(@Param("typeId") int typeId, @Param("articleId") int articleId);

    /**
     * 更新文章标题和内容.
     * @param article 文章信息
     * @return
     */
    @Update("update article set article_title = #{article.articleTitle}, article_context = #{article.articleContext} where article_id = #{article.articleId}")
    int updateArticle(@Param("article") Article article);

    @Delete("delete from article where article_id = #{articleId}")
    int deleteArticle(int articleId);

    /**
     * 添加一个点赞数量
     * @param articleId
     * @return
     */
    @Update("update article set like_counter = like_counter + 1 where article_id = #{articleId}")
    int upLike(int articleId);

    /**
     * 查询文章.
     * @param search 查询关键字
     * @return
     */
    @Select("select * from article where article_title like #{search} order by create_date desc")
    @Results({
            @Result(id = true, property = "articleId", column = "article_id"),
            @Result(property = "types", column = "article_id",
                    many = @Many(select = "hht.dragon.blog.mapper.TypeMapper.getTypesByArticle"))
    })
    List<Article> searchArticle(String search);

}
